﻿package com.dnedelcu.robattle {
    
    import flash.net.Responder;
    import flash.net.NetConnection;
    import flash.events.NetStatusEvent;
    import flash.events.MouseEvent;
    import flash.events.EventDispatcher;
    import flash.text.TextField;
    import flash.display.MovieClip;
	import com.dnedelcu.robattle.GetStatsEvent;
    
    public class Remoting extends EventDispatcher {
        
        private var _netConnection:NetConnection;
        
        private var _parent : Robattle;
        
        public function Remoting(parent_:Robattle) {
            _parent = parent_;
            _netConnection = new NetConnection();
            _netConnection.connect("http://www.robattle.ro/amfphp/");
            _netConnection.addEventListener(NetStatusEvent.NET_STATUS, testConnection);
        }
		
		public function getItemsStats(itemIDs_:Array):void {
			var responder:Responder = new Responder(handleGetItemsStatsResult, handleGetItemsStatsError);
			_netConnection.call("Combat/getItemsStats", responder, itemIDs_);
		}
		
		public function updateUserStats(stats_:Array,charID_:int):void {
			var responder:Responder = new Responder(handleUpdateUserResult, handleUpdateUserError);
			_netConnection.call("Combat/updateUserStats", responder, stats_, charID_);
		}
		
		public function createUserStats(charID_:int):void {
			var responder:Responder = new Responder(handleCreateUserResult, handleCreateUserError);
			_netConnection.call("Combat/getUserStats", responder, charID_);
		}
        
		public function createMonsterStats(charID_:int):void {
			var responder:Responder = new Responder(handleCreateMonsterResult, handleCreateMonsterError);
			_netConnection.call("Combat/getMonsterStats", responder, charID_);
		}
        
		private function handleGetItemsStatsResult(result:Object):void {
			var e:GetStatsEvent = new GetStatsEvent(GetStatsEvent.ITEMS_STATS_RECEIVED, true, true, result);
			dispatchEvent(e);
		}
		
		private function handleGetItemsStatsError():void {
			_parent.debug.htmlText += "error retrieving item stats<br />";
		}
		
        private function handleUpdateUserResult(result:Object):void {
            var e:GetStatsEvent = new GetStatsEvent(GetStatsEvent.USER_STATS_UPDATED, true, true);
            dispatchEvent(e);
			_parent.debug.htmlText += "am facut update la stats";
        }
        
        private function handleUpdateUserError():void {
            _parent.debug.htmlText += 'error updating user stats<br />';
        }
        
        private function handleCreateUserResult(result:Object):void {
			var e:GetStatsEvent = new GetStatsEvent(GetStatsEvent.USER_STATS_CREATED, true,true, result);
            dispatchEvent(e);
        }
        
        private function handleCreateUserError(result:Object):void {
            _parent.debug.htmlText += 'error creating user stats<br />';
            for(var i : * in result) {
				trace(i + " >> " + result[i]);
            }
        }
        
        private function handleCreateMonsterResult(result:Object):void {
            var e:GetStatsEvent = new GetStatsEvent(GetStatsEvent.MONSTER_STATS_CREATED, true,true, result);
            dispatchEvent(e);
        }
        
        private function handleCreateMonsterError(result:Object):void {
            _parent.debug.htmlText += 'error creating monster stats<br />';
            for(var i : * in result) {
				trace(i + " >> " + result[i]);
            }
        }
        
        private function testConnection(event:NetStatusEvent):void {
			trace(event.info.code);
        }
        
    }
}

